package com.xzx.servlet30;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.HandlesTypes;
import java.util.EnumSet;
import java.util.Set;

/**
 * @author xinzhixuan
 * @version 1.0
 * @date 2025/5/10 21:29
 */
@Slf4j
@HandlesTypes({HelloService.class, Servlet.class, Filter.class, ServletContextListener.class})
public class MyServletContainerInitializer implements ServletContainerInitializer {
    /**
     * 容器启动的时候会执行这个方法
     * @param set 使用@HandlesTypes注解指定的类的所有子孙类型(子接口，抽象类，类都可以)，不包含自己
     * @param servletContext .可以使用这个对象注册Servlet、Listener、Filter等
     * @throws ServletException
     */
    @Override
    public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
        log.info("=====MyServletContainerInitializer onStartup");
        set.forEach(clazz -> log.info("=====MyServletContainerInitializer onStartup" + clazz));


        ServletRegistration.Dynamic servlet = servletContext.addServlet("myServlet", MyServlet.class);
        servlet.addMapping("/my");

        servletContext.addListener(MyListener.class);

        FilterRegistration.Dynamic filter = servletContext.addFilter("myFilter", MyFilter.class);
        filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true,"/*");
    }
}
